/**
 * 脚本框架库 - 核心
 * 
 */

(function(){
window["undefined"] = window["undefined"];
var undefined,
	_$ = window.$,
	_GRKZ = window.GRKZ,
	GRKZ = window.GRKZ = window.$ = function(selector, context){
		return new GRKZ.fn.init( selector, context );
	}
	
/**
 * 快捷检测HTML中字符或者ID字符的正则表达式
 */
GRKZ.quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/;

/**
 * 简单选择器的正则表达式
 */
GRKZ.isSimple = /^.[^:#\[\.]*$/;




/**
 * 根对象的方法集合
 * @method fn
 * @static
 */
GRKZ.prototype = {

/**
 * 根对象初始
 * @param {Object} selector		选择器
 * @param {Object} context		选择范围
 */
	init: function(selector,context){
		//默认值 document
		selector = selector || document;
		
		if (selector.nodeType){
			this[0] = selector;
			this.length = 1;
			this.context = selector;
			return this;
		}

		if ( typeof selector === "string"){ //是字符
			
			  var match = GRKZ.quickExpr.exec(selector);
			 
			  if (match && (match[1] || !context)){
			  	
					
					 if (match[1]){
							alert(" TODO: selector = GRKZ.clean( [ match[1] ], context );");
							//TODO selector = GRKZ.clean( [ match[1] ], context );
					 	
					 }else{
					 		
							var elem = document.getElementById(match[3]);

							if (elem && elem.id != match[3]) {
								return GRKZ().find(selector);
							}
							
							var ret = GRKZ(elem || []);
							ret.context = document;
							ret.selector = selector;
							return ret;
						
					 }
					 	
			  }else{
			  		return GRKZ(context).find(selector);
			  }
			  		
		}else if(GRKZ.isFunction(selector)){ //是函数
		    alert(" TODO: GRKZ(document).ready(selector);");
		  //TODO  GRKZ.ready
			//return GRKZ(document).ready(selector);
			
		}else{ //是数组
			return this.setArray(GRKZ.makeArray(selector));
			
		}
		
		
	},
	
/**
 * 将类数组转换成数组
 * @method setArray
 * @param {Object} elems 类数组
 * @return {Array} 返回数组
 */
	setArray:function(elems){
		this.length = 0; //重置length为0
		Array.prototype.push.apply(this,elems);
		return this
	},



/**
 * 将一组对象放置到堆载中，返回一组新的对象
 * @param {Object} elems
 * @param {Object} name
 * @param {Object} selector
 */
	pushStack: function( elems, name, selector ) {
		// Build a new jQuery matched element set
		var ret = GRKZ( elems );
		// Add the old object onto the stack (as a reference)
		ret.prevObject = this;
		ret.context = this.context;
		if ( name === "find" )
			ret.selector = this.selector + (this.selector ? " " : "") + selector;
		else if ( name )
			ret.selector = this.selector + "." + name + "(" + selector + ")";
		// Return the newly-formed element set
		return ret;
	},
	

	
/**
 * 列遍对象
 * @param {Object} elems
 * @param {Object} callback
 */
	map:function( elems, callback ) {
		var ret = [];
		// Go through the array, translating each of the items to their
		// new value (or values).
		for ( var i = 0, length = elems.length; i < length; i++ ) {
			var value = callback( elems[ i ], i );
			alert(value);
			if ( value != null )
				ret[ ret.length ] = value;
		}
		
		return ret.concat.apply( [], ret );
	},

	unique: function( array ) {
		var ret = [], done = {};

		try {

			for ( var i = 0, length = array.length; i < length; i++ ) {
				var id = GRKZ.data( array[ i ] );

				if ( !done[ id ] ) {
					done[ id ] = true;
					ret.push( array[ i ] );
				}
			}

		} catch( e ) {
			ret = array;
		}

		return ret;
	},
	
/**
 * 查找
 * @param {Object} selector
 */
	find: function( selector ) { 
		if ( this.length === 1 && !/,/.test(selector) ) {
			var ret = this.pushStack( [], "find", selector );
			ret.length = 0;
			GRKZ.fn.find( selector, this[0], ret );
			return ret;
		} else { 
			var elems = GRKZ.fn.map(this, function(elem){ return GRKZ.find( selector, elem ); });
			return this.pushStack( /[^+>] [^+>]/.test( selector ) ? GRKZ.unique( elems ) : elems, "find", selector );
		
		}
	},
	
	selector:"",
	version:"1.0.1"
}

GRKZ.prototype.init.prototype = GRKZ.fn = GRKZ.prototype;
  //GRKZ. = GRKZ.fn.map;
 // window.$ = GRKZ;
 
 
GRKZ.extend = GRKZ.fn.extend = function() {
	var target = arguments[0] || {}, // 第一个参数是目标
	i = 1, length = arguments.length, deep = false, options;

	if (target.constructor == Boolean) {// 第一个参数是bool型的
		deep = target;// 深度copy
		target = arguments[1] || {};// target指向第二个参数
		i = 2;
	}

	// target 是string 型的或？
	if (typeof target != "object" && typeof target != "function")
		target = {};

	if (length == i) {// 只有一个参数？或deep copy 时，两个参数
		target = this;// 目标为this
		--i;
	}

	for (;i < length; i++)
		if ((options = arguments[i]) != null)

			for (var name in options) {
				var src = target[name], copy = options[name];
				if (target === copy)// 防止死循环
					continue;
				// 深度copy处理，最深为元素
				if (deep && copy && typeof copy == "object" && !copy.nodeType)
					target[name] = GRKZ.extend(deep, src
							|| (copy.length != null ? [] : {}), copy);
				else if (copy !== undefined)// 直接copy
					target[name] = copy;

			}

	return target;
	
};



	
	
})();

